const Koa = require('koa');

const app = new Koa();

const bodyparser = require('koa-bodyparser');

const router = require('koa-router')();

const query = require('./db/index');

const hmc = require('./util/hmc');

//静态资源
const static = require('koa-static');

const path = require('path');


app.use(static(path.join(__dirname,'./public/')));

//处理前端post和put传递的参数，放置到ctx.request.body
app.use(bodyparser());

app.use(router.routes());

app.use(router.allowedMethods());

//注册

router.post('/api/registry',async ctx => {
    let {username,password,email} = ctx.request.body;
    //容错处理  邮箱的校验
    if(username && password && email){
        //1.排重
        let target = await query('select * from userlist where username=?',[username]);
        console.log(target);//[]
        if(target.length){
            //已经注册
            ctx.body = {
                code:3,
                msg:'此用户名已使用'
            }
        }else{
            //没有注册
            try{
                let hmcPwd = hmc(password);
                await query('insert into userlist (username,password,email) values (?,?,?)',[username,hmcPwd,email]);
                ctx.body = {
                    code:1,
                    msg:'注册成功'
                }
            }catch(e){
                ctx.body = {
                    code:0,
                    msg:e
                }
            }
        }
    }else{
        ctx.body = {
            code:0,
            msg:'参数有误'
        }
    }
})

//登录
router.post('/api/login',async ctx => {
    let {username,password} = ctx.request.body;
    if(username && password){
        let hmcPwd = hmc(password);
        let target = await query('select * from userlist where username=? and password=?',[username,hmcPwd]);
        if(target.length){
            ctx.body = {
                code:1,
                msg:'登录成功'
            }
        }else{
            ctx.body = {
                code:0,
                msg:'登录失败'
            }
        }
    }else{
        ctx.body = {
            code:0,
            msg:'参数有误'
        }
    }
})

//音乐的增删改查

//添加
router.post('/api/add',async ctx => {
    let {songname,songuser,isup} = ctx.request.body;
    if(songname && songuser && isup){
        try{
            await query('insert into musiclist (songname,songuser,isup) values (?,?,?)',[songname,songuser,isup]);
            ctx.body = {
                code:1,
                msg:'添加成功'
            }
        }catch(e){
            ctx.body = {
                code:0,
                msg:e
            }
        }
    }else{
        ctx.body = {
            code:0,
            msg:'参数有误'
        }
    }
})

//修改
router.put('/api/update',async ctx => {
    let {songname,songuser,isup,id} = ctx.request.body;
    if(songname && songuser && isup && id ){
        try{
            await query('update musiclist set songname=?,songuser=?,isup=? where id=?',[songname,songuser,isup,id]);
            ctx.body = {
                code:1,
                msg:'修改成功'
            }
        }catch(e){
            ctx.body = {
                code:0,
                msg:e
            }
        }
    }else{
        ctx.body = {
            code:0,
            msg:'参数有误'
        }
    }
})

//查询  分页  搜索+分页
router.get('/api/list',async ctx => {
    let {pagenum=1,limit=2,key=''} = ctx.query;
    let startIndex = (pagenum-1)*limit; //0 2 4 6
    let list = await query(`select * from musiclist where songname like '%${key}%' limit ${startIndex},${limit}`);
    ctx.body = {
        code:1,
        data:list
    }
})  

//删除
router.delete('/api/del',async ctx => {
    let {id} = ctx.query;
    if(id){
        try{
            await query('delete from musiclist where id=?',[id]);
            ctx.body = {
                code:1,
                msg:'删除成功'
            }
        }catch(e){
            ctx.body = {
                code:0,
                msg:e
            }
        }
    }else{
        ctx.body = {
            code:0,
            msg:'参数有误'
        }
    }
})

//搜索

router.get('/api/search',async ctx => {
    let {key} = ctx.query;
    if(key){
        let list = await query(`select * from musiclist where songname like '%${key}%'`);
        ctx.body = {
            code:1,
            data:list
        }
    }else{
        ctx.body = {
            code:0,
            msg:'参数有误'
        }
    }
})

app.listen(3000,() => {
    console.log("服务启动成功")
})

// sql语句：select * from musiclist where 字段名 like '%${key}%'  %：任意字符